<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/dute_favicon_32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/dute_favicon_16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="manifest" href="/images/manifest.json">
  <meta name="msapplication-config" content="/images/browserconfig.xml">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">
  <meta name="google-site-verification" content="mpI5dkydstZXl6UcDCppqktXK0bbvqdZ6LkZ3KNk4Iw">
  <meta name="baidu-site-verification" content="code-a1LksZX2Ds">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"whitestore.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Linux函数">
<meta property="og:type" content="article">
<meta property="og:title" content="【Linux】Pass arguments into a function">
<meta property="og:url" content="https://whitestore.top/2023/09/13/passarguments/index.html">
<meta property="og:site_name" content="爱看书的阿东">
<meta property="og:description" content="Linux函数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/20230830135648.png">
<meta property="og:image" content="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/20230830140338.png">
<meta property="article:published_time" content="2023-09-13T05:47:10.000Z">
<meta property="article:modified_time" content="2023-10-21T14:26:31.032Z">
<meta property="article:author" content="阿东">
<meta property="article:tag" content="函数">
<meta property="article:tag" content="linux">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/20230830135648.png">

<link rel="canonical" href="https://whitestore.top/2023/09/13/passarguments/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>【Linux】Pass arguments into a function | 爱看书的阿东</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="爱看书的阿东" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">爱看书的阿东</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">赐他一块白色石头，石头上写着新名</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="Follow me on GitHub" aria-label="Follow me on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://whitestore.top/2023/09/13/passarguments/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="阿东">
      <meta itemprop="description" content="随遇而安">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱看书的阿东">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          【Linux】Pass arguments into a function
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-09-13 13:47:10" itemprop="dateCreated datePublished" datetime="2023-09-13T13:47:10+08:00">2023-09-13</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-10-21 22:26:31" itemprop="dateModified" datetime="2023-10-21T22:26:31+08:00">2023-10-21</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Linux/" itemprop="url" rel="index"><span itemprop="name">Linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2023/09/13/passarguments/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2023/09/13/passarguments/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.6k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>
            <div class="post-description">Linux函数</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="Source"><a href="#Source" class="headerlink" title="Source"></a>Source</h1><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUGFzc19hcmd1bWVudHNfaW50b19hX2Z1bmN0aW9u" title="https://bash.cyberciti.biz/guide/Pass_arguments_into_a_function">Pass arguments into a function - Linux Bash Shell Scripting Tutorial Wiki (cyberciti.biz)<i class="fa fa-external-link"></i></span></p>
<p>Let us see how to pass parameters to a Bash function.</p>
<p>让我们看看如何向 Bash 函数传递参数。</p>
<ul>
<li><p>A shell function is nothing but a set of one or more commands/statements that act as a complete routine.</p>
</li>
<li><p>Each function must have a unique name.</p>
</li>
<li><p>Shell functions have their own command line argument or parameters.</p>
</li>
<li><p>Use shell variable <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDE=" title="https://bash.cyberciti.biz/guide/$1">$1<i class="fa fa-external-link"></i></span>, <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDI=" title="https://bash.cyberciti.biz/guide/$2">$2<i class="fa fa-external-link"></i></span>,..$n to access argument passed to the function.</p>
</li>
<li><p>shell 函数是由一条或多条命令/语句组成的一个完整例程。</p>
</li>
<li><p>每个函数都必须有一个唯一的名称。</p>
</li>
<li><p>shell 函数有自己的命令行参数。</p>
</li>
<li><p>使用 shell 变量 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDE=" title="https://bash.cyberciti.biz/guide/$1">$1<i class="fa fa-external-link"></i></span>、<span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDI=" title="https://bash.cyberciti.biz/guide/$2">$2<i class="fa fa-external-link"></i></span>…$n 访问传递给函数的参数。</p>
</li>
</ul>
<a id="more"></a>

<h1 id="Passing-parameters-to-a-Bash-function向-Bash-函数传递参数"><a href="#Passing-parameters-to-a-Bash-function向-Bash-函数传递参数" class="headerlink" title="Passing parameters to a Bash function向 Bash 函数传递参数"></a>Passing parameters to a Bash function向 Bash 函数传递参数</h1><ul>
<li>The syntax is as follows to create user-defined functions in a shell script:</li>
</ul>
<p>在 shell 脚本中创建用户定义函数的语法如下：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="title">function_name</span></span>()&#123;</span><br><span class="line">   command_block_here</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">## OR ##</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">function_name_here</span></span>()&#123;</span><br><span class="line">   command_line_block</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">## passing parameters to a Bash function ##</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="title">my_function_name</span></span>()&#123;</span><br><span class="line">  arg1=<span class="variable">$1</span></span><br><span class="line">  arg2=<span class="variable">$2</span></span><br><span class="line">  <span class="built_in">command</span> on <span class="variable">$arg1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="Invoke-function"><a href="#Invoke-function" class="headerlink" title="Invoke function"></a>Invoke function</h1><ul>
<li>To invoke the the function use the following syntax:</li>
</ul>
<p>要调用该函数，请使用以下语法：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">my_function_name foo bar</span><br></pre></td></tr></table></figure>

<p>Where,</p>
<ol>
<li><p><strong>my_function_name</strong> = Your function name.</p>
</li>
<li><p><strong>foo</strong> = Argument # 1 passed to the function (positional parameter # 1).</p>
</li>
<li><p><strong>bar</strong> = Argument # 2 passed to the function.</p>
</li>
<li><p><strong>my_function_name</strong> = 您的函数名称。</p>
</li>
<li><p><strong>foo</strong> = 传递给函数的参数 # 1（位置参数 # 1）。</p>
</li>
<li><ol start="3">
<li><strong>bar</strong> = 传递给函数的参数 # 2。</li>
</ol>
</li>
</ol>
<h1 id="Examples"><a href="#Examples" class="headerlink" title="Examples"></a>Examples</h1><p>Create a function called fresh.sh:</p>
<p>创建一个名为 fresh.sh 的函数：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"> </span><br><span class="line"><span class="comment"># write a function</span></span><br><span class="line"><span class="function"><span class="title">fresh</span></span>()&#123;</span><br><span class="line">   <span class="comment"># t stores $1 argument passed to fresh()</span></span><br><span class="line">   t=<span class="variable">$1</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): \$0 is <span class="variable">$0</span>"</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): \$1 is <span class="variable">$1</span>"</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): \$t is <span class="variable">$t</span>"</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): total args passed to me <span class="variable">$#</span>"</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): all args (\$@) passed to me -\"<span class="variable">$@</span>\""</span></span><br><span class="line">   <span class="built_in">echo</span> <span class="string">"fresh(): all args (\$*) passed to me -\"$*\""</span></span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="comment"># invoke the function with "Tomato" argument</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"**** calling fresh() 1st time ****"</span></span><br><span class="line">fresh Tomato</span><br><span class="line"> </span><br><span class="line"><span class="comment"># invoke the function with total 3 arguments</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"**** calling fresh() 2nd time ****"</span></span><br><span class="line">fresh Tomato Onion Paneer</span><br></pre></td></tr></table></figure>

<p>Save and close the file. Run it as follows:</p>
<p>保存并关闭文件。按以下步骤运行：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chmod +x fresh.sh</span><br><span class="line">./fresh.sh</span><br></pre></td></tr></table></figure>

<p>Sample outputs:</p>
<p>样本输出：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">**** calling fresh() 1st time ****</span><br><span class="line">fresh(): <span class="variable">$0</span> is ./fresh.sh</span><br><span class="line">fresh(): <span class="variable">$1</span> is Tomato</span><br><span class="line">fresh(): <span class="variable">$t</span> is Tomato</span><br><span class="line">fresh(): total args passed to me 1</span><br><span class="line">fresh(): all args (<span class="variable">$@</span>) passed to me -<span class="string">"Tomato"</span></span><br><span class="line">fresh(): all args ($*) passed to me -<span class="string">"Tomato"</span></span><br><span class="line">**** calling fresh() 2nd time ****</span><br><span class="line">fresh(): <span class="variable">$0</span> is ./fresh.sh</span><br><span class="line">fresh(): <span class="variable">$1</span> is Tomato</span><br><span class="line">fresh(): <span class="variable">$t</span> is Tomato</span><br><span class="line">fresh(): total args passed to me 3</span><br><span class="line">fresh(): all args (<span class="variable">$@</span>) passed to me -<span class="string">"Tomato Onion Paneer"</span></span><br><span class="line">fresh(): all args ($*) passed to me -<span class="string">"Tomato Onion Paneer"</span></span><br></pre></td></tr></table></figure>

<p>Let us try one more example. Create a new shell script to determine if given name is file or directory (cmdargs.sh):</p>
<p>让我们再试一个例子。创建一个新的 shell 脚本来判断给定的名称是文件还是目录（cmdargs.sh）：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Name - cmdargs.sh</span></span><br><span class="line"><span class="comment"># Purpose - Passing positional parameters to user defined function </span></span><br><span class="line"><span class="comment"># 目的 - 将位置参数传递给用户定义函数 </span></span><br><span class="line"><span class="comment"># -----------------------------------------------------------------</span></span><br><span class="line">file=<span class="string">"<span class="variable">$1</span>"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># User-defined function</span></span><br><span class="line"><span class="function"><span class="title">is_file_dir</span></span>()&#123;</span><br><span class="line">        <span class="comment"># $f is local variable</span></span><br><span class="line">	<span class="built_in">local</span> f=<span class="string">"<span class="variable">$1</span>"</span></span><br><span class="line">        <span class="comment"># file attributes comparisons using test i.e. [ ... ]</span></span><br><span class="line">	[ -f <span class="string">"<span class="variable">$f</span>"</span> ] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> is a regular file."</span>; <span class="built_in">exit</span> 0; &#125;</span><br><span class="line">	[ -d <span class="string">"<span class="variable">$f</span>"</span> ] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> is a directory."</span>; <span class="built_in">exit</span> 0; &#125;</span><br><span class="line">	[ -L <span class="string">"<span class="variable">$f</span>"</span> ] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> is a symbolic link."</span>; <span class="built_in">exit</span> 0; &#125;</span><br><span class="line">	[ -x <span class="string">"<span class="variable">$f</span>"</span> ] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"<span class="variable">$f</span> is an executeble file."</span>; <span class="built_in">exit</span> 0; &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># make sure filename supplied as command line arg else die</span></span><br><span class="line"><span class="comment"># 确保文件名作为命令行参数提供，否则 die</span></span><br><span class="line">[ <span class="variable">$#</span> -eq 0 ] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> filename"</span>; <span class="built_in">exit</span> 1; &#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># invoke the is_file_dir and pass $file as arg</span></span><br><span class="line"><span class="comment"># 调用 is_file_dir，并将 $file 作为参数传入</span></span><br><span class="line">is_file_dir <span class="string">"<span class="variable">$file</span></span></span><br></pre></td></tr></table></figure>

<p>Run it as follows:</p>
<p>运行方法如下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">./cmdargs.sh /etc/resolv.conf</span><br><span class="line">./cmdargs.sh /bin/date</span><br><span class="line">./cmdargs.sh <span class="variable">$HOME</span></span><br><span class="line">./cmdargs.sh /sbin</span><br></pre></td></tr></table></figure>

<p>Sample outputs:</p>
<p>样本输出：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/etc/resolv.conf is a regular file.</span><br><span class="line">/bin/date is a regular file.</span><br><span class="line">/home/vivek is a directory.</span><br><span class="line">/sbin is a directory.</span><br></pre></td></tr></table></figure>

<h1 id="Function-shell-variables"><a href="#Function-shell-variables" class="headerlink" title="Function shell variables"></a>Function shell variables</h1><ul>
<li><p>All function parameters or arguments can be accessed via $1, $2, $3,…, $N.</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDA=" title="https://bash.cyberciti.biz/guide/$0">$0<i class="fa fa-external-link"></i></span> always point to the shell script name.</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJCo=" title="https://bash.cyberciti.biz/guide/$*">$*<i class="fa fa-external-link"></i></span> or <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJEA=" title="https://bash.cyberciti.biz/guide/$@">$@<i class="fa fa-external-link"></i></span> holds all parameters or arguments passed to the function.</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJA==" title="https://bash.cyberciti.biz/guide/$">$#<i class="fa fa-external-link"></i></span> holds the number of positional parameters passed to the function.</p>
</li>
<li><p>所有函数参数或参数都可以通过<code>$1, $2, $3,..., $N</code> 访问。</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDA=" title="https://bash.cyberciti.biz/guide/$0">$0<i class="fa fa-external-link"></i></span>总是指向 shell 脚本名。</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJCo=" title="https://bash.cyberciti.biz/guide/$*">$* <i class="fa fa-external-link"></i></span>  或  <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJEA=" title="https://bash.cyberciti.biz/guide/$@">$@<i class="fa fa-external-link"></i></span> 保存传递给函数的所有参数。</p>
</li>
<li><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJA==" title="https://bash.cyberciti.biz/guide/$">$#<i class="fa fa-external-link"></i></span> 保存传递给函数的位置参数的个数。</p>
</li>
</ul>
<h2 id="How-do-I-display-function-name-如何显示功能名称？"><a href="#How-do-I-display-function-name-如何显示功能名称？" class="headerlink" title="How do I display function name? 如何显示功能名称？"></a>How do I display function name? 如何显示功能名称？</h2><p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDA=" title="https://bash.cyberciti.biz/guide/$0">$0<i class="fa fa-external-link"></i></span> always point to the shell script name. However, you can use an array variable called <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9RlVOQ05BTUUmYWN0aW9uPWVkaXQmcmVkbGluaz0x" title="https://bash.cyberciti.biz/wiki/index.php?title=FUNCNAME&action=edit&redlink=1">FUNCNAME<i class="fa fa-external-link"></i></span> which contains the names of all shell functions currently in the execution call stack. The element with index 0 is the name any currently-executing shell function.This variable <em>exists only</em> when a shell function is executing.</p>
<p><span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvJDA=" title="https://bash.cyberciti.biz/guide/$0">$0<i class="fa fa-external-link"></i></span>总是指向 shell 脚本名。不过，您可以使用一个名为 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9RlVOQ05BTUUmYWN0aW9uPWVkaXQmcmVkbGluaz0x" title="https://bash.cyberciti.biz/wiki/index.php?title=FUNCNAME&action=edit&redlink=1">FUNCNAME<i class="fa fa-external-link"></i></span>的数组变量，它包含当前执行调用堆栈中所有 shell 函数的名称。索引为 0 的元素是当前正在执行的 shell 函数的名称。</p>
<p><strong>FUNCNAME in action</strong></p>
<p>Create a shell script called funcback.sh:</p>
<p>创建名为 funcback.sh 的 shell 脚本：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment">#  funcback.sh : Use $FUNCNAME</span></span><br><span class="line"><span class="function"><span class="title">backup</span></span>()&#123;</span><br><span class="line">	<span class="built_in">local</span> d=<span class="string">"<span class="variable">$1</span>"</span></span><br><span class="line">	[[ -z <span class="variable">$d</span> ]] &amp;&amp; &#123; <span class="built_in">echo</span> <span class="string">"<span class="variable">$&#123;FUNCNAME&#125;</span>(): directory name not specified"</span>; <span class="built_in">exit</span> 1; &#125;</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">"Starting backup..."</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">backup <span class="variable">$1</span></span><br></pre></td></tr></table></figure>

<p>Save and close the file. Run it as follows:</p>
<p>保存并关闭文件。按以下步骤运行：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">chmod +x funcback.sh</span><br><span class="line">funcback.sh /home</span><br><span class="line">funcback.sh</span><br></pre></td></tr></table></figure>

<p>Sample outputs:</p>
<p>样本输出：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">backup(): directory name not specified</span><br></pre></td></tr></table></figure>

<p>Return values</p>
<p>返回值</p>
<p>It is possible to pass a value from the function back to the bash using the <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span>. The return statement terminates the function. The syntax is as follows:</p>
<p>可以使用 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span>将函数值传回 bash。return 语句终止函数。语法如下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">return</span></span><br><span class="line"><span class="built_in">return</span> [value]</span><br></pre></td></tr></table></figure>

<p>One can force script to exit with the return value specified by [value]. If [value] is omitted, the return status is that of the last command executed within the function or script.</p>
<p>可以强制脚本以 [value] 指定的返回值退出。如果省略 [value]，返回状态将是函数或脚本中最后执行的命令的状态。</p>
<h2 id="Examples-1"><a href="#Examples-1" class="headerlink" title="Examples"></a>Examples</h2><p>Create a new file called math.sh:</p>
<p>新建一个名为 math.sh 的文件：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Name - math.sh</span></span><br><span class="line"><span class="comment"># Purpose - Demo return value </span></span><br><span class="line"><span class="comment"># ------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## user define function</span></span><br><span class="line"><span class="function"><span class="title">math</span></span>()&#123;</span><br><span class="line">	<span class="built_in">local</span> a=<span class="variable">$1</span></span><br><span class="line">	<span class="built_in">local</span> b=<span class="variable">$2</span></span><br><span class="line">	<span class="built_in">local</span> sum=$(( a + b))</span><br><span class="line">	<span class="built_in">return</span> <span class="variable">$sum</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">## call the math function with 5 and 10 as  arguments </span></span><br><span class="line">math 5 10</span><br><span class="line"></span><br><span class="line"><span class="comment">## display back result (returned value) using $?</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"5 + 10 = $?"</span></span><br></pre></td></tr></table></figure>

<p>Run it as follows:</p>
<p>运行方法如下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">chmod +x math.sh</span><br><span class="line">./math.sh</span><br></pre></td></tr></table></figure>

<p><img src="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/20230830135648.png" alt="image.png"></p>
<p>The above example is straightforward. The <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span> returns any given value between <strong>zero (0) and 255</strong>. By default, the value passed by the return statement is the current value of the <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvVGhlX2V4aXRfc3RhdHVzX29mX2FfY29tbWFuZA==" title="https://bash.cyberciti.biz/guide/The_exit_status_of_a_command">exit status<i class="fa fa-external-link"></i></span> variable. For example, the following code will return wrong values as it is not between <strong>zero (0) and 255</strong>.</p>
<p>上面的例子很简单。<span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return 命令<i class="fa fa-external-link"></i></span> 返回介于<strong>零 (0) 和 255</strong> 之间的任意给定值。默认情况下，return 语句传递的值是 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvVGhlX2V4aXRfc3RhdHVzX29mX2FfY29tbWFuZA==" title="https://bash.cyberciti.biz/guide/The_exit_status_of_a_command">exit status<i class="fa fa-external-link"></i></span> 变量的当前值。例如，以下代码将返回错误的值，因为它不在<strong>零（0）和 255</strong> 之间。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">math 500 100</span><br></pre></td></tr></table></figure>

<p>Here is the proper use of <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span> (bash-task.sh):</p>
<p>下面是 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span>的正确用法（bash-task.sh）：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Name: bash-task.sh</span></span><br><span class="line"><span class="comment"># Purpose: Check if a file exists or not using custom bash function and return value</span></span><br><span class="line"><span class="comment"># -----------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># set values </span></span><br><span class="line"><span class="built_in">readonly</span> TRUE=0</span><br><span class="line"><span class="built_in">readonly</span> FALSE=1</span><br><span class="line"></span><br><span class="line"><span class="comment"># get input from the CLI</span></span><br><span class="line">file=<span class="string">"<span class="variable">$1</span>"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># return $TRUE (0) if file found </span></span><br><span class="line"><span class="comment"># return $FALSE (1) if file not found</span></span><br><span class="line"><span class="function"><span class="title">is_file_found</span></span>()&#123;</span><br><span class="line">	[ -f <span class="string">"<span class="variable">$1</span>"</span> ] &amp;&amp; <span class="built_in">return</span> <span class="variable">$TRUE</span> || <span class="built_in">return</span> <span class="variable">$FALSE</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># show usage info if $1 not passed to our script</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$#</span> -eq 0 ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> filename"</span></span><br><span class="line">	<span class="built_in">exit</span> 1</span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># let us call our function </span></span><br><span class="line">is_file_found <span class="string">"<span class="variable">$file</span>"</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># take action based upon return value</span></span><br><span class="line"><span class="keyword">if</span> [ $? -eq 0 ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">"<span class="variable">$file</span> added to backup task"</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">"<span class="variable">$file</span> not found."</span></span><br><span class="line"><span class="keyword">fi</span></span><br></pre></td></tr></table></figure>

<p>Run it as follows:</p>
<p>运行方法如下</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">chmod +x bash-task.sh</span><br><span class="line">./bash-task.sh</span><br><span class="line">./bash-task.sh /etc/passwd</span><br><span class="line">./bash-task.sh /foo/bar</span><br></pre></td></tr></table></figure>

<p><img src="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/20230830140338.png" alt="image.png"></p>
<blockquote>
<p>Passing parameters to a Bash function and return true or false value<br>向 Bash 函数传递参数并返回 true 或 false 值</p>
</blockquote>
<h1 id="How-to-return-custom-value-from-a-user-defined-function"><a href="#How-to-return-custom-value-from-a-user-defined-function" class="headerlink" title="How to return custom value from a user-defined function?"></a>How to return custom value from a user-defined function?</h1><p>As I said earlier, you can’t use <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span>. However, the workaround is as following using the <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUHJpbnRmX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Printf_command">printf command<i class="fa fa-external-link"></i></span>/<span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRWNob19jb21tYW5k" title="https://bash.cyberciti.biz/guide/Echo_command">echo command<i class="fa fa-external-link"></i></span>:</p>
<p>正如前面所说，不能使用 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUmV0dXJuX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Return_command">return command<i class="fa fa-external-link"></i></span>。不过，变通方法如下：使用 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvUHJpbnRmX2NvbW1hbmQ=" title="https://bash.cyberciti.biz/guide/Printf_command">printf 命令<i class="fa fa-external-link"></i></span>/<span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRWNob19jb21tYW5k" title="https://bash.cyberciti.biz/guide/Echo_command">echo命令<i class="fa fa-external-link"></i></span>：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Name - math.sh</span></span><br><span class="line"><span class="comment"># Purpose - Demo how to return custom value </span></span><br><span class="line"><span class="comment"># -----------------------------------------</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## user define function that use echo/printf </span></span><br><span class="line"><span class="function"><span class="title">math</span></span>()&#123;</span><br><span class="line">	<span class="built_in">local</span> a=<span class="variable">$1</span></span><br><span class="line">	<span class="built_in">local</span> b=<span class="variable">$2</span></span><br><span class="line">	<span class="built_in">local</span> sum=$(( a + b))</span><br><span class="line">	<span class="built_in">echo</span> <span class="variable">$sum</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">## call the math function with 5 and 10 as  arguments </span></span><br><span class="line">total=$(math 500 42)</span><br><span class="line"></span><br><span class="line"><span class="comment">## display back result (returned value) using $?</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">"500 + 42 = <span class="variable">$total</span>"</span></span><br></pre></td></tr></table></figure>
<h1 id="Listing-functions-列出函数"><a href="#Listing-functions-列出函数" class="headerlink" title="Listing functions 列出函数"></a>Listing functions 列出函数</h1><p>Use the <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9VHlwZXNldF9jb21tYW5kJmFjdGlvbj1lZGl0JnJlZGxpbms9MQ==" title="https://bash.cyberciti.biz/wiki/index.php?title=Typeset_command&action=edit&redlink=1">typeset command<i class="fa fa-external-link"></i></span> or <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRGVjbGFyZV9jb21tYW5k" title="https://bash.cyberciti.biz/guide/Declare_command">declare command<i class="fa fa-external-link"></i></span>.</p>
<p>使用 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9VHlwZXNldF9jb21tYW5kJmFjdGlvbj1lZGl0JnJlZGxpbms9MQ==" title="https://bash.cyberciti.biz/wiki/index.php?title=Typeset_command&action=edit&redlink=1">typeset command<i class="fa fa-external-link"></i></span>或 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRGVjbGFyZV9jb21tYW5k" title="https://bash.cyberciti.biz/guide/Declare_command">declare command<i class="fa fa-external-link"></i></span>。</p>
<h1 id="Show-the-known-functions-and-their-code-definitions显示已知函数及其代码-定义"><a href="#Show-the-known-functions-and-their-code-definitions显示已知函数及其代码-定义" class="headerlink" title="Show the known functions and their code/definitions显示已知函数及其代码/定义"></a>Show the known functions and their code/definitions显示已知函数及其代码/定义</h1><p>Open the terminal and then run:</p>
<p>打开终端，然后运行：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">declare</span> -f</span><br><span class="line"><span class="built_in">declare</span> -f function_name_here</span><br><span class="line"><span class="comment">## or ##</span></span><br><span class="line"><span class="built_in">typeset</span> -f</span><br><span class="line"><span class="built_in">typeset</span> -f function_name_here</span><br></pre></td></tr></table></figure>
<h1 id="How-to-remove-or-unset-functions-如何删除或取消设置函数"><a href="#How-to-remove-or-unset-functions-如何删除或取消设置函数" class="headerlink" title="How to remove or unset functions 如何删除或取消设置函数"></a>How to remove or unset functions 如何删除或取消设置函数</h1><p>Use the <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9VW5zZXRfY29tbWFuZCZhY3Rpb249ZWRpdCZyZWRsaW5rPTE=" title="https://bash.cyberciti.biz/wiki/index.php?title=Unset_command&action=edit&redlink=1">unset command<i class="fa fa-external-link"></i></span> to unset values and attributes of <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvVmFyaWFibGVz" title="https://bash.cyberciti.biz/guide/Variables">shell variables<i class="fa fa-external-link"></i></span> and <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRnVuY3Rpb25z" title="https://bash.cyberciti.biz/guide/Functions">functions<i class="fa fa-external-link"></i></span>:</p>
<p>使用 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovd2lraS9pbmRleC5waHA/dGl0bGU9VW5zZXRfY29tbWFuZCZhY3Rpb249ZWRpdCZyZWRsaW5rPTE=" title="https://bash.cyberciti.biz/wiki/index.php?title=Unset_command&action=edit&redlink=1">unset 命令<i class="fa fa-external-link"></i></span> 取消设置 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvVmFyaWFibGVz" title="https://bash.cyberciti.biz/guide/Variables">shell 变量<i class="fa fa-external-link"></i></span> 和 <span class="exturl" data-url="aHR0cHM6Ly9iYXNoLmN5YmVyY2l0aS5iaXovZ3VpZGUvRnVuY3Rpb25z" title="https://bash.cyberciti.biz/guide/Functions">函数<i class="fa fa-external-link"></i></span> 的值和属性：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">unset</span> -f function_name_here</span><br><span class="line"><span class="built_in">unset</span> -f foo</span><br><span class="line"><span class="built_in">unset</span> -f bar</span><br></pre></td></tr></table></figure>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>lazytime
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://whitestore.top/2023/09/13/passarguments/" title="【Linux】Pass arguments into a function">https://whitestore.top/2023/09/13/passarguments/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLzQuMC96aC1DTg=="><i class="fa fa-fw fa-creative-commons"></i>BY-NC</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Linux/" rel="tag"># Linux</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2023/09/11/adaptg1gc/" rel="prev" title="【JVM】Garbage First Garbage Collector Tuning">
      <i class="fa fa-chevron-left"></i> 【JVM】Garbage First Garbage Collector Tuning
    </a></div>
      <div class="post-nav-item">
    <a href="/2023/09/13/zerocopy/" rel="next" title="【Linux】Linux Zero-Copy Using sendfile">
      【Linux】Linux Zero-Copy Using sendfile <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Source"><span class="nav-number">1.</span> <span class="nav-text">Source</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Passing-parameters-to-a-Bash-function向-Bash-函数传递参数"><span class="nav-number">2.</span> <span class="nav-text">Passing parameters to a Bash function向 Bash 函数传递参数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Invoke-function"><span class="nav-number">3.</span> <span class="nav-text">Invoke function</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Examples"><span class="nav-number">4.</span> <span class="nav-text">Examples</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Function-shell-variables"><span class="nav-number">5.</span> <span class="nav-text">Function shell variables</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#How-do-I-display-function-name-如何显示功能名称？"><span class="nav-number">5.1.</span> <span class="nav-text">How do I display function name? 如何显示功能名称？</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Examples-1"><span class="nav-number">5.2.</span> <span class="nav-text">Examples</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#How-to-return-custom-value-from-a-user-defined-function"><span class="nav-number">6.</span> <span class="nav-text">How to return custom value from a user-defined function?</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Listing-functions-列出函数"><span class="nav-number">7.</span> <span class="nav-text">Listing functions 列出函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Show-the-known-functions-and-their-code-definitions显示已知函数及其代码-定义"><span class="nav-number">8.</span> <span class="nav-text">Show the known functions and their code&#x2F;definitions显示已知函数及其代码&#x2F;定义</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#How-to-remove-or-unset-functions-如何删除或取消设置函数"><span class="nav-number">9.</span> <span class="nav-text">How to remove or unset functions 如何删除或取消设置函数</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">阿东</p>
  <div class="site-description" itemprop="description">随遇而安</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">239</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2xhenlUaW1lcw==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;lazyTimes"><i class="fa fa-fw fa-github"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOjEwOTc0ODM1MDhAcXEuY29t" title="E-Mail → mailto:1097483508@qq.com"><i class="fa fa-fw fa-envelope"></i>E-Mail</span>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly93d3cuNTJwb2ppZS5jbi9ob21lLnBocD9tb2Q9c3BhY2UmdWlkPTE0OTc3MTgmZG89dGhyZWFkJnZpZXc9bWUmZnJvbT1zcGFjZQ==" title="https:&#x2F;&#x2F;www.52pojie.cn&#x2F;home.php?mod&#x3D;space&amp;uid&#x3D;1497718&amp;do&#x3D;thread&amp;view&#x3D;me&amp;from&#x3D;space">吾爱破解</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9qdWVqaW4uaW0vdXNlci8yOTk5MTIzNDUyNjI2MzY2" title="https:&#x2F;&#x2F;juejin.im&#x2F;user&#x2F;2999123452626366">掘金</span>
        </li>
        <li class="links-of-blogroll-item">
          <span class="exturl" data-url="aHR0cHM6Ly9zZWdtZW50ZmF1bHQuY29tL3UvbGF6eXRpbWVz" title="https:&#x2F;&#x2F;segmentfault.com&#x2F;u&#x2F;lazytimes">思否</span>
        </li>
    </ul>
  </div>

      </div>

      <div class="wechat_OA">
        <span>欢迎关注我的公众号</span>
        <br>
          <!-- 这里添加你的二维码图片 -->
        <img src ="https://adong-picture.oss-cn-shenzhen.aliyuncs.com/adong/wechat_channel.jpg">
      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">阿东</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">2m</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">29:50</span>
</div>
  <div class="powered-by">由 <span class="exturl theme-link" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & <span class="exturl theme-link" data-url="aHR0cHM6Ly90aGVtZS1uZXh0Lm9yZw==">NexT.Gemini</span> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : true,
      appId      : 'qMUpEEvBgXaMDD1b0ftgi9xr-gzGzoHsz',
      appKey     : 'UCdfT4Rfih6MO6y8DI4fstf6',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
